using Cooking.Core.Model;

namespace Cooking.UnitTest.Helpers
{
    public class RecipeBuilder
    {
        private readonly Recipe _recipe = new Recipe();

        private RecipeBuilder(string name)
        {
            _recipe = new Recipe { Id = 100, Name = name };
        }

        public static RecipeBuilder Create(string recipeName)
        {
            return new RecipeBuilder(recipeName);
        }

        public RecipeBuilder WithIngredient(string ingredientName, string prep)
        {
            var ingredient = new Ingredient {Id = _recipe.Ingredients.Count + 1, Name = ingredientName};
            var ri = new RecipeIngredient { BelongsTo = _recipe, 
                                            Id = _recipe.Ingredients.Count + 1, 
                                            Ingredient = ingredient, 
                                            IngredientPrep = prep, 
                                            Quantity = 1, 
                                            UnitOfMeasure = "cup"};
            
            
            _recipe.AddIngredient(ri);

            return this;
        }


        public static RecipeBuilder HamSandwich()
        {
            return Create("Ham Sandwich")
                .WithIngredient("Bread", "Sliced")
                .WithIngredient("Ham", "Shaved");
        }

        public static RecipeBuilder PeanutButterAndJelly()
        {
            return Create("PB&J")
                .WithIngredient("Bread", "Sliced")
                .WithIngredient("Peanut butter", string.Empty)
                .WithIngredient("Jelly", "Smashed")
                .AddStep("Spread peanut buuter on one slice of bread")
                .AddStep("Spread jelly on the other slice of bread");
        }

        public static implicit operator Recipe(RecipeBuilder fromBuilder)
        {
            return fromBuilder._recipe;
        }

        public RecipeBuilder AddStep(string description)
        {
            _recipe.AddStep(new PreparationStep {BelongsTo = _recipe, Description = description, Id = _recipe.Steps.Count + 1, SequenceNumber = _recipe.Steps.Count + 1});
            return this;
        }
    }
}